package com.study.d014Bounds;

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.BoxBlur;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
*@description 坐标转换
*@author xtayhame
*date 2020年3月4日
*version
*since v1.0
*/
public class BoundsMain extends Application{
	public static void main(String[] args) {

		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane =new AnchorPane();
		Button b1 = new Button("这是我的第一个按钮");

		//设置旋转的度数，设置后
		b1.setRotate(90);
		//设置阴影，磨砂
		b1.setEffect(new BoxBlur());

		HBox hb = new HBox();
		hb.setStyle("-fx-background-color:#ffff11");
		hb.setAlignment(Pos.CENTER);
		hb.setPrefWidth(200);
		hb.setPrefHeight(200);
		hb.getChildren().add(b1);

		anchorPane.getChildren().add(hb);
		AnchorPane.setTopAnchor(hb, 30.0);
		AnchorPane.setLeftAnchor(hb, 40.0);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();

		System.out.println("按钮的宽：" + b1.getWidth() + " ，按钮的高：" + b1.getHeight());

		//注意：获取组件的坐标时，需要在show()方法后面进行获取

		//获取相对于父组件的左上角的坐标
		System.out.println("getLayoutX()="+b1.getLayoutX() + ", b1.getLayoutY()=" +b1.getLayoutY());

		//获取相对于Button按钮自身的坐标
		Bounds localBound= b1.getLayoutBounds();
		System.out.println("相对本组件：----->左上角 X="+localBound.getMinX()+ ",左上角 Y=" + localBound.getMinY() + ", 右下角 X="+localBound.getMaxX() + " ,右下角 Y=" + localBound.getMaxY() + " 宽：" + localBound.getWidth() + " , 高：" + localBound.getHeight());


		//将button某一点，转换为相对父容器的坐标
		Point2D localPoint= b1.localToParent(localBound.getMinX() , localBound.getMinY());
		System.out.println("相对父容器：X=" + localPoint.getX() + ",Y=" + localPoint.getY());


		//将Botton某一点，转换为相对场景的坐标
		Point2D scenePoint=b1.localToScene(localBound.getMinX() , localBound.getMinY());
		System.out.println("相对父场景：X=" + scenePoint.getX() + ",Y=" + scenePoint.getY());

		//将Button某一点，转换为相对电脑屏幕的坐标
		Point2D screenPoint=b1.localToScreen(localBound.getMinX() , localBound.getMinY());
		System.out.println("相对电脑屏幕：X=" + screenPoint.getX() + ",Y=" + screenPoint.getY());


		//获取组件自身的Bounds
		Bounds layoutBound= b1.getLayoutBounds();
		System.out.println("获取组件自身的Bounds:"+layoutBound);

		//获取组件自身装饰后的Bounds，比如设置了setEffect()后
		Bounds localBound2= b1.getBoundsInLocal();
		System.out.println("获取组件自身装饰后的Bounds:"+localBound2);

		//获取组件父组件的Bounds
		Bounds parentBound= b1.getBoundsInParent();
		System.out.println("获取组件父组件的Bounds:"+parentBound);
	}

}
